package tuding.android.bigplanettracks;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import tuding.android.bigplanettracks.maps.Logex;
//import android.util.Log;


public class SimpleLocationService extends Service implements LocationListener {

	private NotificationManager mNotificationManager;
	public SimpleLocationService() {
		
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Logex.message("Recording", "Service: onCreate()");
		showNotification();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Logex.message("Recording", "Service: onDestroy()");
		clearNotification();
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO update the lat/lng/alt for upload usage
		if(location == null || location.getLatitude() == 0 || location.getLongitude() == 0) return;
		if((location.hasAccuracy() && location.getAccuracy()<1000) || !location.hasAccuracy()) {
		//if(true){
			Logex.message("Recording", "location is changed with acc:"+location.getAccuracy());
			BigPlanet.clocationRecord = location;
			Message msg = new Message();
			msg.what = 3;
			BigPlanet.updateLocationUpdateHandler.sendMessage(msg);
			if(BigPlanet.simpleLocationService != null) {
				BigPlanet.locationManagerforRecord.removeUpdates(BigPlanet.simpleLocationService);
			}
		}
		
		Logex.message("Recording", "onLocationChanged acc: "+location.getAccuracy());
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	
	private void showNotification() {
		int iconId = 0;
		String contentTitle = null;
		String contentText = null;
		
		iconId = R.drawable.recording;
		contentTitle = getString(R.string.app_name);
		contentText = getString(R.string.notify_uploading);
		
		Intent notifyIntent = new Intent(this, BigPlanet.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(
				this, 0, notifyIntent, Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

		Notification notification = new Notification();
		notification.flags = Notification.FLAG_NO_CLEAR;
		notification.icon = iconId;
		
		notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent);
		startForeground(2, notification);
		mNotificationManager.notify(2, notification);
	}
	
	private void clearNotification() {
		stopForeground(true);
		mNotificationManager.cancel(2);
	}
	
	
}